/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.grassinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.DateUtils;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.common.utils.excel.ExportExcel;
import com.longview.io.modules.grassinfo.entity.UserGrasslandInfo;
import com.longview.io.modules.grassinfo.service.UserGrasslandInfoService;
import com.longview.io.modules.livestockinfo.entity.UserLivestockInfo;

/**
 * 草场信息Controller
 * @author zf
 * @version 2018-04-23
 */
@Controller
@RequestMapping(value = "${adminPath}/grassinfo/userGrasslandInfo")
public class UserGrasslandInfoController extends BaseController {

	@Autowired
	private UserGrasslandInfoService userGrasslandInfoService;
	
	@ModelAttribute
	public UserGrasslandInfo get(@RequestParam(required=false) String id) {
		UserGrasslandInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = userGrasslandInfoService.get(id);
		}
		if (entity == null){
			entity = new UserGrasslandInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("grassinfo:userGrasslandInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(UserGrasslandInfo userGrasslandInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UserGrasslandInfo> page = userGrasslandInfoService.findPage(new Page<UserGrasslandInfo>(request, response), userGrasslandInfo); 
		model.addAttribute("page", page);
		return "modules/grassinfo/userGrasslandInfoList";
	}

	@RequiresPermissions("grassinfo:userGrasslandInfo:view")
	@RequestMapping(value = "form")
	public String form(UserGrasslandInfo userGrasslandInfo, Model model) {
		
		userGrasslandInfo.setCoordinates(userGrasslandInfo.getCoordinates().replaceAll("\"", "")); 
		model.addAttribute("userGrasslandInfo", userGrasslandInfo);
		return "modules/grassinfo/userGrasslandInfoForm";
	}
	/**
	 * 导出用户数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("grassinfo:userGrasslandInfo:view")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(UserGrasslandInfo userGrasslandInfo, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "草场数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<UserGrasslandInfo> page = userGrasslandInfoService.findPage(new Page<UserGrasslandInfo>(request, response), userGrasslandInfo); 
    			new ExportExcel("草场数据", UserGrasslandInfo.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出草场失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/grassinfo/userGrasslandInfo/?repage";
    }
	@RequiresPermissions("grassinfo:userGrasslandInfo:edit")
	@RequestMapping(value = "save")
	public String save(UserGrasslandInfo userGrasslandInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, userGrasslandInfo)){
			return form(userGrasslandInfo, model);
		}
		userGrasslandInfoService.save(userGrasslandInfo);
		addMessage(redirectAttributes, "保存草场信息成功");
		return "redirect:"+Global.getAdminPath()+"/grassinfo/userGrasslandInfo/?repage";
	}
	
	@RequiresPermissions("grassinfo:userGrasslandInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(UserGrasslandInfo userGrasslandInfo, RedirectAttributes redirectAttributes) {
		userGrasslandInfoService.delete(userGrasslandInfo);
		addMessage(redirectAttributes, "删除草场信息成功");
		return "redirect:"+Global.getAdminPath()+"/grassinfo/userGrasslandInfo/?repage";
	}

}